<script setup lang="ts">
import {
  getDeviceListAPI,
  getDeviceDetailSerialAPI,
  addDeviceAPI,
  deleteDeviceAPI
} from "@/api/destribute.ts";
import {onMounted, reactive, ref} from "vue";
import {deviceItem} from "@/type/destribute.ts";
import {ElMessage, ElNotification} from "element-plus";
import {Delete, Edit, InfoFilled} from "@element-plus/icons-vue";

// 获取设备列表
const deviceList = ref<deviceItem[]>()
const getDeviceData = async () => {
  const res = await getDeviceListAPI()
  deviceList.value = res.data
}

// 搜索设备
const serialNumber = ref('')
const deviceDetail = ref<deviceItem>()
const dialogDeviceDetailVisible = ref(false)
const searchDevice = async () => {
  dialogDeviceDetailVisible.value = true
  const res = await getDeviceDetailSerialAPI(serialNumber.value)
  deviceDetail.value = res.data
}

// 添加设备
const dialogAddDeviceVisible = ref(false)
const deviceData = reactive({
  name: '',
  serialNumber: '',
  purchaseDate: '',
  purchasePrice: ''
})
const addDevice = async () => {
  dialogAddDeviceVisible.value = false
  const res = await addDeviceAPI(deviceData)
  await getDeviceData()
  ElMessage({
    type: 'success',
    message: res.message
  })
}

// 删除设备
const deleteDevice = async (id) => {
  const res = await deleteDeviceAPI(id)
  await getDeviceData()
  ElMessage({
    type: 'success',
    message: res.message
  })
}

// 编辑设备
const EditEquip = () => {
  ElNotification({
    title: 'Warning',
    message: '为减缓服务器压力，暂关闭编辑接口及数据回显',
    type: 'warning',
  })
}

onMounted(() => {
  getDeviceData()
})
</script>

<template>
  <el-card class="box-card">
    <div class="card-equip-title">
      <div class="row align_center">
        <el-input v-model="serialNumber" clearable placeholder="请输入型号"/>
        <el-button style="margin-left: 20px" type="primary" @click="searchDevice">搜索设备</el-button>
      </div>
      <el-button @click="dialogAddDeviceVisible=true">添加设备</el-button>
    </div>
    <div class="card-equip-table">
      <el-table
          border
          class="el-table-box"
          ref="multipleTableRef"
          :data="deviceList"
          style="width: 100%"
      >
        <el-table-column label="#" width="60" align="center" type="index"/>
        <el-table-column prop="name" align="center" label="名称"/>
        <el-table-column prop="purchaseDate" align="center" label="购买日期"/>
        <el-table-column prop="purchasePrice" align="center" label="购买金额" width="100"/>
        <el-table-column prop="serialNumber" align="center" label="型号"/>
        <el-table-column prop="status" align="center" label="状态" width="100"/>
        <el-table-column align="center" label="控制" width="180">
          <template #default="{row}">
            <div class="operator-btn">
              <el-button type="primary" size="default" :icon="Edit as any" @click="EditEquip" circle/>
              <el-popconfirm
                  width="200"
                  @confirm="deleteDevice(row._id)"
                  confirm-button-text="确定"
                  cancel-button-text="取消"
                  :icon="InfoFilled as any"
                  icon-color="#f36b6c"
                  title="您确定删除此设备吗?"
              >
                <template #reference>
                  <el-button type="danger" size="default" :icon="Delete as any" circle/>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </el-card>

  <!--  详情对话框-->
  <el-dialog :close-on-click-modal="false" v-model="dialogDeviceDetailVisible" title="设备详情">
    <div class="dialog-item">
      <span>名称：</span>
      <p>{{ deviceDetail?.name }}</p>
    </div>
    <div class="dialog-item">
      <span>购买时间：</span>
      <p>{{ deviceDetail?.purchaseDate }}</p>
    </div>
    <div class="dialog-item">
      <span>价格：</span>
      <p>{{ deviceDetail?.purchasePrice }}</p>
    </div>
    <div class="dialog-item">
      <span>型号：</span>
      <p>{{ deviceDetail?.serialNumber }}</p>
    </div>
    <div class="dialog-item">
      <span>状态：</span>
      <p>{{ deviceDetail?.status }}</p>
    </div>
  </el-dialog>

  <!-- 添加设备 -->
  <el-dialog :close-on-click-modal="false" v-model="dialogAddDeviceVisible" title="添加设备">
    <el-form
        :model="deviceData"
        status-icon
        class="demo-ruleForm row"
    >
      <div>
        <el-form-item label="名称" prop="name">
          <el-input v-model="deviceData.name" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="型号" prop="serialNumber">
          <el-input v-model="deviceData.serialNumber" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="价格" prop="purchasePrice">
          <el-input v-model.number="deviceData.purchasePrice" autocomplete="off"/>
        </el-form-item>
      </div>
      <div style="margin-left: 20px" class="date-select-div">
        <el-form-item label="日期" prop="purchaseDate">
          <el-input v-model="deviceData.purchaseDate" placeholder="年-月-日格式" autocomplete="off"/>
        </el-form-item>
        <el-button type="primary" class="confirm-btn" @click="addDevice">确认</el-button>
      </div>
    </el-form>
  </el-dialog>
</template>

<style scoped lang="scss">
.box-card {
  padding: 0 50px;

  .card-equip-title {
    height: 35px;
    display: flex;
    justify-content: space-between;
  }

  .card-equip-table {
    margin-top: 20px;

    .el-table-box {
      height: calc(90vh - 140px);

      .el-table__cell {
        position: static !important; // 解决el-image 和 el-table冲突层级冲突问题
      }
    }

    .equip-desc {
      display: -webkit-box; /*弹性伸缩盒子*/
      -webkit-box-orient: vertical; /*垂直排列*/
      line-height: 20px;
      -webkit-line-clamp: 1;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}

.dialog-item {
  display: flex;
  flex-direction: row;
  height: 30px;
  align-items: center;

  p {
    margin-left: 12px;
  }
}

.demo-ruleForm {
  padding: 0 60px;
}

.date-select-div {
  position: relative;

  .confirm-btn {
    position: absolute;
    bottom: 10px;
    right: 0;
  }
}
</style>